`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 11/17/2021 06:05:24 PM
// Design Name: 
// Module Name: long_short_selector
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 用于切换设置模块中按键输入的是长按信号还是短按信号，包含了时钟分频、选择器的功能
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module long_short_selector(
    input key_short,
    input clk,
    input key_last,
    input key_value,
    input rst_n,
    output reg key_out
    );
    reg [50:0] m;
    reg isLong;
    initial begin
        m=20'b0;
        key_out = 1'b0;
    end
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            isLong <= 0;
        end
        else if(key_last)
            isLong <= 1;
        else if(key_value==0 && isLong==1)
            isLong <= 0;
    end
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) m =0;
        else if (m==10000001) m=0;
        else m = m+1;
    end

	always @(posedge clk)
	if (isLong) begin
	    if(m==10000000) //可通过更改条件值改变脉冲周期
            key_out = 1;
        else
            key_out = 0;
    end
    else 
        key_out = key_short;

endmodule
